package cc.rengu.igas.smps.core.service.trans;

import cc.rengu.igas.smps.common.constant.SmpsParamConstant;
import cc.rengu.igas.smps.common.constant.SmpsServiceConstant;
import cc.rengu.igas.smps.common.constant.SmpsTreeNodeConstant;
import cc.rengu.igas.smps.facade.request.SunyardVoicePushRequest;
import cc.rengu.igas.smps.facade.response.SunyardVoicePushResponse;
import cc.rengu.jradp.schedule.ScheduleImpl;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;

/**
 * 信雅达语音消息推送服务
 * Created by wangcheng on 2021/4/2 10:34
 */
public class SunyardVoicePushService extends RadpService {
    @Override
    protected int bizInit(String s) {
        return 0;
    }

    @Override
    protected int bizProcess(String s, int i) {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        SunyardVoicePushRequest sunyardVoicePushRequest = new SunyardVoicePushRequest();
        SunyardVoicePushResponse sunyardVoicePushResponse = new SunyardVoicePushResponse();
        try {
            ConvertUtil.convertOutput(sunyardVoicePushRequest);
            sunyardVoicePushResponse.setHeader(sunyardVoicePushRequest.getHeader());
            String payType = sunyardVoicePushRequest.getPayType();
            String payAmt = sunyardVoicePushRequest.getPayAmt();
            if (StringUtil.isNullorEmpty(payType) || StringUtil.isNullorEmpty(payAmt)) {
                rglog.error("缺少一个或多个业务参数！");
                sunyardVoicePushResponse.setRespCode(OltpRpcdEnum.PARAM_IS_EMPTY_OR_NULL.getRespCode());
                sunyardVoicePushResponse.setRespDesc(OltpRpcdEnum.PARAM_IS_EMPTY_OR_NULL.getRespDesc());
            } else {
                /* 演示环境发送短信默认返回成功 */
                if (AppParamConstant.DEFAULT_ENV.equals(AppParamConstant.APPLICATION_ENV)) {
                    sunyardVoicePushResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
                    sunyardVoicePushResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
                } else {
                    int iReturnCode = schedule.callService(SmpsServiceConstant.SUNYARD_COMMUNICATION_CLIENT, SmpsParamConstant.DEFAULT_SERVICE_LABEL);
                    if (ScheduleImpl.SERVICE_SUCCESS == iReturnCode) {
                        String code = xmlTreeUtil.getXmlTreeStringValue(SmpsTreeNodeConstant.XYDVOICE_CODE);
                        if ("000000".equals(code)) {
                            /* 推送成功 */
                            sunyardVoicePushResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
                            sunyardVoicePushResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
                        } else {
                            /* 失败 调用超时 赋值应答 */
                            rglog.error("获取TOKEN {} 失败！返回值={}", SmpsServiceConstant.SUNYARD_COMMUNICATION_CLIENT, iReturnCode);
                            sunyardVoicePushResponse.setRespCode(code);
                            String message = xmlTreeUtil.getXmlTreeStringValue(SmpsTreeNodeConstant.XYDVOICE_MESSAGE);
                            if (!StringUtil.isNullorEmpty(message)) {
                                sunyardVoicePushResponse.setRespDesc(message);
                            }
                        }
                    } else if (ScheduleImpl.SERVICE_TIMEOUT == iReturnCode) {
                        rglog.error("callService {} 失败！返回值={}", SmpsServiceConstant.SUNYARD_COMMUNICATION_CLIENT, iReturnCode);
                        sunyardVoicePushResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
                        sunyardVoicePushResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
                    } else {
                        rglog.error("callService {} 失败！返回值={}", SmpsServiceConstant.SUNYARD_COMMUNICATION_CLIENT, iReturnCode);
                        sunyardVoicePushResponse.setRespCode(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespCode());
                        sunyardVoicePushResponse.setRespDesc(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
                    }
                }
            }
            ConvertUtil.convertInput(sunyardVoicePushResponse);
        } catch (Exception e) {
            //失败赋值应答码为明确失败及应答消息直接退出
            rglog.error("{}", StringUtil.ExceptionToString(e));
            sunyardVoicePushResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            sunyardVoicePushResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            return -1;
        }
        return 0;
    }

    @Override
    protected int bizTerminate(String s) {
        return 0;
    }
}
